const app = getApp()
import util from '../../utils/util'
import {
  GetGoodsScore,
  ExchangeGoods,
  GetCanGoodsScore
} from '../../config/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vollege_id: '',
    contents: ['全部商品', '我能兑换的'], //切换内容
    active: 0, //默认激活第一个选项
    testarr: [1, 1, 1, 1, 1, 1, 1, 1, 1], //测试循环数组
    goodsList: [], //积分兑换商品数据
  },

  /**
   * 点击弹出兑换
   */
  DuiHuan: function (e) {

    let member = wx.getStorageSync('member')
    let grider_storge = wx.getStorageSync('grider')
    console.log('网格角色：', !grider_storge)

    if (grider_storge && !member) {
      wx.showModal({
        showCancel: false,
        content: '您不是村民，暂不能操作此功能'
      })
      return;
    } else if (!grider_storge && !member) {
      wx.redirectTo({
        url: '/pages/register/register',
      })
      return;
    }

    let item = e.currentTarget.dataset['item'];
    // console.log('您点击了兑换item！',item);
    wx.showModal({
      title: '提示',
      content: '确认兑换吗？',
      success(res) {
        if (res.confirm) {
          //let member = wx.getStorageSync('member');
          console.log('uid:', member.id);
          console.log('name:', member.name);
          console.log('code:', member.code);
          let uid = member.id;
          let home_code = member.code;
          let u_name = member.name;
          let params = {
            goods_id: item.id,
            uid: uid,
            home_code: home_code,
            u_name: u_name,
            page:1,
            limit:100
          };
          util.apiRequest(ExchangeGoods, params, null).then(r => {
            console.log('兑换rrr', r)
            if (r.data.code == 1) {
              wx.showModal({
                showCancel: false,
                content: r.data.info,
                success(res) {
                  if (res.confirm) {
                    console.log('兑换确认')
                  }
                }
              })
              // wx.navigateTo({
              //   url: '/pages/success/success?type=' + 'store'
              // }); //如果成功跳转到ok页面

            } else {
              wx.showToast({
                title: r.data.info,
                icon: "none",
                duration: 1500
              })
            }
            console.log('兑换商品结果：', r.data.code, '***r.info', r.data.info);
          });

          console.log('用户点击确定');

        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });

  },
  /**
   * 改变切换选项卡默认激活状态
   */
  SwitchStatus: function (e) {
    let member = wx.getStorageSync('member')
    let grider_storge = wx.getStorageSync('grider')

    if (grider_storge && !member) {
      wx.showModal({
        showCancel: false,
        content: '您不是村民，暂不能操作此功能'
      })
      return;
    } else if (!grider_storge && !member) {
      wx.redirectTo({
        url: '/pages/register/register',
      })
      return;
    }

    // 传递的参数
    let index = e.currentTarget.dataset['index'];
    // console.log('点击事件传递的参数：',index);
    let that = this;
    that.setData({
      active: index,
    });
    if (index == 1) {
      let params = {
        vollege_id: that.data.vollege_id,
        code: member.code,
        page:1,
        limit:100
      };
      util.apiRequest(GetCanGoodsScore, params, null).then(r => {
        console.log('我能兑换的记录', r.data.data.data);
        that.setData({
          goodsList: r.data.data.data,
        });
      });
    } else {
      this.getAllGoods();
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app)
    this.setData({
      vollege_id: app.globalData.vollege_id
    })
    this.getAllGoods();
  },

  getAllGoods() {
    let that = this;
    util.apiRequest(GetGoodsScore, {
      vollege_id: this.data.vollege_id
    }, null).then(r => {
      console.log('积分商城列表', r);
      that.setData({
        goodsList: r.data.data.data,
      });
    });
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})